package com.example.myys.viewModel.ysModel

import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.myys.Constants
import com.example.myys.data.entity.DataBean
import com.example.myys.data.repository.MovieRepository
import com.example.myys.ui.Resource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class YsViewModel :ViewModel(){
    val movieRepository = MovieRepository
    var state = mutableStateOf(0);

    //推荐
    private val _viedoState:MutableStateFlow<Resource<List<DataBean>>>
        =MutableStateFlow(value = Resource.Loading())

    val videoState:StateFlow<Resource<List<DataBean>>>
        get() = _viedoState
    //收藏
    private val _sjState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Loading())

    val sjState: StateFlow<Resource<List<DataBean>>>
        get() = _sjState
    //搜索
    private val _ssState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Error())

    val ssState: StateFlow<Resource<List<DataBean>>>
        get() = _ssState

    //电视剧
    private val _dsjState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Loading())

    val dsjState: StateFlow<Resource<List<DataBean>>>
        get() = _dsjState
    //电影
    private val _dyState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Loading())

    val dyState: StateFlow<Resource<List<DataBean>>>
        get() = _dyState
    //动漫
    private val _dmState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Loading())

    val dmState: StateFlow<Resource<List<DataBean>>>
        get() = _dmState
    //综艺
    private val _zyState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Loading())

    val zyState: StateFlow<Resource<List<DataBean>>>
        get() = _zyState
    //短剧
    private val _djState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Loading())

    val djState: StateFlow<Resource<List<DataBean>>>
        get() = _djState
    //llp
    private val _llpState: MutableStateFlow<Resource<List<DataBean>>>
            = MutableStateFlow(value = Resource.Loading())

    val llpState: StateFlow<Resource<List<DataBean>>>
        get() = _llpState


    fun getMovieRecommend(){
        viewModelScope.launch(Dispatchers.IO) {
            val movieList=movieRepository.getMovieRecommend()
            if(movieList!=null){
                _viedoState.value=Resource.Success(movieList)
            }else{
                _viedoState.value=Resource.Error()
            }
        }
    }



    fun getMoiveShelf(){
        _sjState.value=Resource.Loading()
        viewModelScope.launch(Dispatchers.IO) {
            val sjList= movieRepository.getMovieShelf()
            if(sjList==null){
                _sjState.value=Resource.Error()
            }else{
                _sjState.value= Resource.Success(sjList)
            }

        }
    }
    fun addMovieShelf(DataBean: DataBean){
        viewModelScope.launch(Dispatchers.IO) {
            movieRepository.addMovieShelf(DataBean)
            getMoiveShelf()
        }
    }
    fun deleteMovieShelf(dataBean: DataBean) {
        viewModelScope.launch(Dispatchers.IO) {
            movieRepository.deleteShelf(dataBean)
            getMoiveShelf()
        }
    }

    fun searchMovie(keyWord:String){
        _ssState.value=Resource.Loading()
        viewModelScope.launch(Dispatchers.IO) {
            val sjList= movieRepository.getMovieByKeyword(keyWord)
            if(sjList==null){
                _ssState.value=Resource.Error()
            }else{
                _ssState.value= Resource.Success(sjList)
            }

        }
    }
    var dsjpage = 1
    fun getDsjDataListByType(page: Int = 0) {
        if (_dsjState.value.data?.isNotEmpty() == true && page == 0) {
            return
        }
        if (page == 0) {
            _dsjState.value = Resource.Loading()
        } else {
            dsjpage = dsjpage + 1
            _dsjState.value = Resource.Loading(_dsjState.value.data)
        }
        viewModelScope.launch(Dispatchers.IO) {
            val sjList = movieRepository.getMovieByType(Constants.YS_DSJ, dsjpage)
            if(sjList==null){
                _dsjState.value=Resource.Error()
            }else{
                if (page == 0) {
                    _dsjState.value = Resource.Success(sjList)
                } else {
                    _dsjState.value = Resource.Success(_dsjState.value.data?.plus(sjList) ?: sjList)
                }
            }

        }
    }

    var dypage = 1
    fun getDyDataListByType(page: Int = 0) {
        if (_dyState.value.data?.isNotEmpty() == true && page == 0) {
            return
        }
        if (page == 0) {
            _dyState.value = Resource.Loading()
        } else {
            dypage = dypage + 1
            _dyState.value = Resource.Loading(_dyState.value.data)
        }
        viewModelScope.launch(Dispatchers.IO) {
            val sjList = movieRepository.getMovieByType(Constants.YS_DY, dypage)
            if(sjList==null){
                _dyState.value=Resource.Error()
            }else{
                if (page == 0) {
                    _dyState.value = Resource.Success(sjList)
                } else {
                    _dyState.value = Resource.Success(_dyState.value.data?.plus(sjList) ?: sjList)
                }
            }

        }
    }

    var dmpage = 1
    fun getDmDataListByType(page: Int = 0) {
        if (_dmState.value.data?.isNotEmpty() == true && page == 0) {
            return
        }
        if (page == 0) {
            _dmState.value = Resource.Loading()
        } else {
            dmpage = dmpage + 1
            _dmState.value = Resource.Loading(_dmState.value.data)
        }
        viewModelScope.launch(Dispatchers.IO) {
            val sjList = movieRepository.getMovieByType(Constants.YS_DM, dmpage)
            if(sjList==null){
                _dmState.value=Resource.Error()
            }else{
                if (page == 0) {
                    _dmState.value = Resource.Success(sjList)
                } else {
                    _dmState.value = Resource.Success(_dmState.value.data?.plus(sjList) ?: sjList)
                }
            }

        }
    }

    var zypage = 1
    fun getZyDataListByType(page: Int = 0) {
        if (_zyState.value.data?.isNotEmpty() == true && page == 0) {
            return
        }
        if (page == 0) {
            _zyState.value = Resource.Loading()
        } else {
            zypage = zypage + 1
            _zyState.value = Resource.Loading(_zyState.value.data)
        }
        viewModelScope.launch(Dispatchers.IO) {
            val sjList = movieRepository.getMovieByType(Constants.YS_ZY, zypage)
            if(sjList==null){
                _zyState.value=Resource.Error()
            }else{
                if (page == 0) {
                    _zyState.value = Resource.Success(sjList)
                } else {
                    _zyState.value = Resource.Success(_zyState.value.data?.plus(sjList) ?: sjList)
                }
            }

        }
    }

    var djpage = 1
    fun getDjDataListByType(page: Int = 0) {
        if (_djState.value.data?.isNotEmpty() == true && page == 0) {
            return
        }
        if (page == 0) {
            _djState.value = Resource.Loading()
        } else {
            djpage = djpage + 1
            _djState.value = Resource.Loading(_djState.value.data)
        }
        viewModelScope.launch(Dispatchers.IO) {
            val sjList = movieRepository.getMovieByType(Constants.YS_DJ, djpage)
            if(sjList==null){
                _djState.value=Resource.Error()
            }else{
                if (page == 0) {
                    _djState.value = Resource.Success(sjList)
                } else {
                    _djState.value = Resource.Success(_djState.value.data?.plus(sjList) ?: sjList)
                }
            }

        }
    }

    var llppage = 1
    fun getLlpDataListByType(page: Int = 0) {
        if (_llpState.value.data?.isNotEmpty() == true && page == 0) {
            return
        }
        if (page == 0) {
            _llpState.value = Resource.Loading()
        } else {
            llppage = llppage + 1
            _llpState.value = Resource.Loading(_llpState.value.data)
        }
        viewModelScope.launch(Dispatchers.IO) {
            val sjList = movieRepository.getMovieByType("LLP", llppage)
            if(sjList==null){
                _llpState.value=Resource.Error()
            }else{
                if (page == 0) {
                    _llpState.value = Resource.Success(sjList)
                } else {
                    _llpState.value = Resource.Success(_llpState.value.data?.plus(sjList) ?: sjList)
                }
            }

        }
    }

    init {
        getMovieRecommend()
        getMoiveShelf()
    }
}